package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.AdminUser;
import com.banksoft.cps.bean.Police;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.PoliceQuery;
import com.banksoft.cps.constants.ErrorConstant;
import com.banksoft.cps.constants.WebConstant;
import com.banksoft.cps.dao.PoliceDao;
import com.banksoft.cps.enums.UserType;
import com.banksoft.cps.service.PoliceService;
import com.banksoft.cps.util.CacheUtil;
import com.banksoft.cps.util.StrGenerator;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rongzhao.zhu on 2018/11/12.
 */
@Service
public class PoliceServiceImpl implements PoliceService {
    @Autowired
    PoliceDao policeDao;
    @Override
    public List<Police> list(PoliceQuery query) {
        return policeDao.list(query);
    }

    @Override
    public Police find(String id) {
        return policeDao.find(id);
    }

    @Override
    public void insert(Police bean) {
        policeDao.insert(bean);
    }

    @Override
    public void update(Police bean) {
        policeDao.update(bean);
    }

    @Override
    public long count(PoliceQuery query) {
        return policeDao.count(query);
    }

    @Override
    public void save(Police bean) {
        if(StringUtils.isNotBlank(bean.getId())){
            policeDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            policeDao.insert(bean);
        }
    }

    @Override
    public void setAccountAndPassword(Police bean) {
        Police policeOld =  policeDao.find(bean.getId());
        if(StringUtils.isNotBlank(policeOld.getAccount())){
            policeOld.setPassword(bean.getPassword());
        }else {
            policeOld.setAccount(bean.getAccount());
            policeOld.setPassword(bean.getPassword());
        }
        policeDao.update(policeOld);
    }

    @Override
    public Police login(String account, String password) throws ServiceException {
            if(StringUtils.isBlank(account)){
                throw new ServiceException(ErrorConstant.ec_param_null,ErrorConstant.em_user_account_null);
            }
            if(StringUtils.isBlank(password)){
                throw new ServiceException(ErrorConstant.ec_param_null,ErrorConstant.em_user_password_null);
            }
            Police adminUser = policeDao.findByAccount(account);
            if(adminUser == null ){
                throw new ServiceException(ErrorConstant.ec_result_null,ErrorConstant.em_user_null);
            }
            if(!adminUser.getPassword().equals(password)){
                throw new ServiceException(ErrorConstant.ec_param_error,ErrorConstant.em_password_error);
            }
            String token =  token(adminUser);
            Map<String,String> cookieMap = new HashMap<>();
            cookieMap.put(WebConstant.cookieToken,token);
            cookieMap.put(WebConstant.cookieFake, StrGenerator.generateRandomStr(32));
            cookieMap.put(WebConstant.cookieName, StrGenerator.EncoderByMd5(adminUser.getName()));
            adminUser.setCookieMap(cookieMap);
            adminUser.setType(UserType.subSecurity.name());
            CacheUtil.cache(token,adminUser);

            return adminUser;
    }

    @Override
    public void updatePassword(String id, String oldPassword, String newPassword) throws ServiceException {

    }

    @Override
    public void delete(String[] ids) {
        policeDao.delete(ids);
    }
}
